<?php

require_once 'lib/ffwd/prototypes/view.clistex.php';

class ClistExAjaxView	 extends ClistExView{ 
	public function construct($p){
		parent::__construct($p);
	}
	
	public function renderBody($resultset,$p){
		if($this->table){
			$pk=$this->table->getPrimaryKey();
		}else if($this->e){
			$pk=$this->e->getPrimaryKey()->name;
		}else{
			$pk='id';			
		};
		$st='';
		$resultset->data_seek(0);
		$i=0;
		if($this->rowTemplateFn && !file_exists($this->rowTemplateFn)){
			$tpl="<tr>\n";
			foreach($p['order'] as $k){
				$tpl.="	<td><?=\$row['$k'];?></td>\n";				
			}
			$tpl.="</tr>\n";		
			console_log('Template generated: '.$this->rowTemplateFn);
			file_put_contents($this->rowTemplateFn, $tpl);	
		};
		while($row=$resultset->fetch_assoc()){
			if($this->table){$st=$this->table->formatRow($row);};
			
			if($this->rowTemplateFn){
				include $this->rowTemplateFn;
				continue;
			}
			
			if(isset($p['rowclickcallback'])){
				echo '<tr class="tr" onclick="'.($p['rowclickcallback']($row)).'" '.$st.'>'."\n";
			}else{
				echo '<tr class="tr" href="'.link_for($row[$pk].'/'.$p[ACTION]).'" '.$st.'>'."\n";
			};
						
			foreach($p['order'] as $k){
				if($k=='id')continue;
				if(isset($row[$k])){						
					if(isset($p['limit']) && isset($p['limit'][$k]) && strlen($row[$k])>$p['limit'][$k]){
						$row[$k]=substr($row[$k],0,$p['limit'][$k]).'&#8230;';
					}
					echo '<td class="td" '.$this->getTDStyle($p,$k).'>'.
						str_replace(' ','&nbsp;',
							(isset($p['limit'][$k])?substr($row[$k],0,$p['limit'][$k]).(strlen($row[$k])>$p['limit'][$k]?'&hellip;':''):$row[$k]).
						' ').
						'</td>';
				}else{
					echo '<td  class="td" '.$this->getTDStyle($p,$k).'>&nbsp;</td>'."\n";
				};					
			};
			echo '</tr>'."\n";
			
			if(++$i==$this->pages->entryperpage)break;
		};
		
	}


}
